package joc.internal;

import joc.internal.compiler.StaticCall;
import joc.internal.util.ObjectMapper;

public class ConditionCache {
	private static final ThreadLocal<ObjectMapper<String, ConditionWrapper>> conditionCache = new ThreadLocal<ObjectMapper<String, ConditionWrapper>>() {
		@Override
		protected ObjectMapper<String, ConditionWrapper> initialValue() {
			return new ObjectMapper<String, ConditionWrapper>();
		}
	};

	public static final StaticCall saveCondition = new StaticCall(ConditionCache.class, "saveCondition");
	public static final StaticCall containsCondition = new StaticCall(ConditionCache.class, "containsCondition");
	public static final StaticCall getCondition = new StaticCall(ConditionCache.class, "getCondition");
	public static final StaticCall activateCondition = new StaticCall(ConditionCache.class, "activateCondition");
	public static final StaticCall deactivateCondition = new StaticCall(ConditionCache.class, "deactivateCondition");
	public static final StaticCall isConditionActive = new StaticCall(ConditionCache.class, "isConditionActive");

	public static void saveCondition(Object obj, String key, Object condition) {
		conditionCache.get().put(obj, key, new ConditionWrapper(condition));
	}

	public static boolean containsCondition(Object obj, String key) {
		return conditionCache.get().contains(obj, key);
	}

	public static Object getCondition(Object obj, String key) {
		return conditionCache.get().get(obj, key).getCondition();
	}

	public static void activateCondition(Object obj, String key) {
		conditionCache.get().get(obj, key).setActive(true);
	}

	public static void deactivateCondition(Object obj, String key) {
		conditionCache.get().get(obj, key).setActive(false);
	}

	public static boolean isConditionActive(Object obj, String key) {
		return conditionCache.get().get(obj, key).isActive();
	}
}
